//
//  Favorites.m
//
//  Created by sid on 11/05/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Favorites.h"

@implementation Favorites

- (void) viewDidLoad
{
	[super viewDidLoad];
	self.title = @"Favoritos";
	
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	favorites = [[NSMutableDictionary alloc] initWithDictionary:[defaults dictionaryForKey:@"myFavorites"]];
	
	editButton = [[UIBarButtonItem alloc] initWithTitle:@"Editar"
													style:UIBarButtonSystemItemEdit 
												   target:self
												   action:@selector(editTable)];
	
	self.navigationItem.rightBarButtonItem = editButton;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{	
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)
	
	
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
	{
        return 80;
    } 	
    else 
	{
		return 55;
	}
	
	
#endif
}

- (void)editTable
{
	if (self.tableView.editing)
	{
		[self.tableView setEditing:NO animated:YES];
		editButton.title = @"Editar";
	}
	else 
	{
		[self.tableView setEditing:YES animated:YES];
		editButton.title = @"Hecho";
	}
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
	return [favorites count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSMutableDictionary *dict = (NSMutableDictionary *)[[favorites allValues] objectAtIndex:indexPath.row];
	MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:dict];
	
	static NSString *cellIdentifier = @"Cell";
	
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
	if(cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
	}
	cell.textLabel.text = annotation.title;
	cell.detailTextLabel.text = annotation.subtitle;
	cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"ico_fav_%@.png", annotation.service]];
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	[annotation release];
	
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)
	
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
	{
		cell.textLabel.font = [UIFont boldSystemFontOfSize:30];
    } 	
	
#endif
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSMutableDictionary *dict = (NSMutableDictionary *)[[favorites allValues] objectAtIndex:indexPath.row];
	MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:dict];
	DetailsView *infoView = [[DetailsView alloc] initWithNibName:@"DetailsView" bundle:nil];
	[infoView setAnnotation:annotation];
	[annotation release];
	[self.navigationController pushViewController:infoView animated:YES];
	[infoView release];
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{
	return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
	if (editingStyle == UITableViewCellEditingStyleDelete) 
	{
		NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
		NSMutableDictionary *favs = [[NSMutableDictionary alloc] initWithDictionary:[defaults dictionaryForKey:@"myFavorites"]];
		NSString *key = (NSString *)[[favorites allKeys] objectAtIndex:indexPath.row];
		[favs removeObjectForKey:key];
		
		[defaults setObject:favs forKey:@"myFavorites"];
		[defaults synchronize];
		
		[favorites removeAllObjects];
		[favorites addEntriesFromDictionary:favs];
		[self.tableView reloadData];
		[favs release];
	}
}

/*
- (NSMutableArray *)reversedArray:(NSMutableArray *)array
{
    NSEnumerator *enumerator = [array reverseObjectEnumerator];
	
    for (id element in enumerator) 
	{
        [array addObject:element];
    }
    return array;
}
 */

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{	
	return @"Borrar";
}

- (void) dealloc
{
	[editButton release];
	[super dealloc];
}

@end